package com.share.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.CabinetSlot;
import com.share.device.domain.PowerBank;
import com.share.device.domain.vo.CabinetSlotVo;
import com.share.device.service.CabinetService;
import com.share.device.service.CabinetSlotService;
import com.share.device.service.PowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Tag(name = "充电宝插槽接口管理")
@RestController
@RequestMapping("/cabinetSlot")
public class CabinetSlotController extends BaseController {
    @Autowired
    private CabinetSlotService cabinetSlotService;
    @Autowired
    private PowerBankService powerBankService;
    @Autowired
    private CabinetService cabinetService;
    @GetMapping("/{id}")
    @Operation(summary = "根据柜机id查询充电宝插槽信息")
    @Transactional
    public AjaxResult getInfo(@PathVariable Long id){
        List<CabinetSlotVo> cabinetSlotVos = new ArrayList<>();
        //根据柜机id查询充电宝插槽信息
        List<CabinetSlot> list = cabinetSlotService.list(new LambdaQueryWrapper<CabinetSlot>().eq(CabinetSlot::getCabinetId, id));
        //判断插槽信息中的充电宝信息是否为空
        list.forEach(item -> {
            //将查询出的插槽信息中的插槽编号给插槽vo
            CabinetSlotVo cabinetSlotVo = new CabinetSlotVo();
            cabinetSlotVo.setSlotNo(item.getSlotNo());
            cabinetSlotVo.setId(item.getId());
            if(item.getPowerBankId() != null){
                //根据充电宝id查询充电宝信息
                cabinetSlotVo.setPowerBank(powerBankService.getById(item.getPowerBankId()));
            }else{
                //充电宝为空
                cabinetSlotVo.setPowerBank(null);
            }
            cabinetSlotVos.add(cabinetSlotVo);
        });
        return success(cabinetSlotVos);
    }

    /**
     * 给充电宝插槽添加充电宝
     * @param shotId
     * @param powerBankId
     * @return
     */
    @GetMapping("/addPowerBank/{shotId}/{powerBankId}")
    @Operation(summary = "给充电宝插槽添加充电宝")
    @Transactional
    public AjaxResult addPowerBank(@PathVariable Long shotId, @PathVariable Long powerBankId){
        //根据插槽id查询插槽信息
        CabinetSlot cabinetSlot = cabinetSlotService.getById(shotId);
        if(cabinetSlot == null){
            return error("插槽不存在");
        }
        //判断插槽是否已添加充电宝
        if(cabinetSlot.getPowerBankId() != null){
            return error("插槽已添加充电宝");
        }
        //判断充电宝是否已添加插槽
        CabinetSlot cabinetSlot1 = cabinetSlotService.getOne(new LambdaQueryWrapper<CabinetSlot>().eq(CabinetSlot::getPowerBankId, powerBankId));
        if(cabinetSlot1 != null){
            return error("充电宝已添加插槽");
        }
        cabinetSlot.setPowerBankId(powerBankId);
        //修改插槽状态为占用
        cabinetSlot.setStatus("1");
        boolean update = cabinetSlotService.updateById(cabinetSlot);
        if(update){
            //更新充电宝状态
            PowerBank powerBank = powerBankService.getById(powerBankId);
            powerBank.setStatus("1");
            powerBankService.updateById(powerBank);
        }
        //给柜机空闲插槽数 已使用插槽数 和可用充电宝数更新
        Cabinet cabinet = cabinetService.getOne(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getId, cabinetSlot.getCabinetId()));
        //给空闲插槽数-1
        cabinet.setFreeSlots(cabinet.getFreeSlots() - 1);
        //给已使用插槽数+1
        cabinet.setUsedSlots(cabinet.getUsedSlots() + 1);
        //给可用充电宝数+1
        cabinet.setAvailableNum(cabinet.getAvailableNum() + 1);
        cabinet.setUpdateTime(new Date());
        cabinetService.updateById(cabinet);
        return update ? success("添加成功") : error("添加失败");
    }
    /**
     * 移除充电宝插槽的充电宝
     */
    @GetMapping("/removePowerBank/{shotId}")
    @Operation(summary = "移除充电宝插槽的充电宝")
    @Transactional
    public AjaxResult removePowerBank(@PathVariable Long shotId) {
        CabinetSlot cabinetSlot = cabinetSlotService.getById(shotId);
        if(cabinetSlot == null){
            return error("插槽不存在");
        }
        //修改充电宝的状态为未使用
        PowerBank powerBank = powerBankService.getById(cabinetSlot.getPowerBankId());
        powerBank.setStatus("0");
        powerBankService.updateById(powerBank);
        //移除充电宝
        cabinetSlot.setPowerBankId(null);
        //修改插槽的状态为空闲
        cabinetSlot.setStatus("0");
        cabinetSlotService.updateById(cabinetSlot);
        //给柜机空闲插槽数 已使用插槽数 和可用充电宝数更新
        Cabinet cabinet = cabinetService.getOne(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getId, cabinetSlot.getCabinetId()));
        //给空闲插槽数+1
        cabinet.setFreeSlots(cabinet.getFreeSlots() + 1);
        //给已使用插槽数-1
        cabinet.setUsedSlots(cabinet.getUsedSlots() - 1);
        //给可用充电宝数-1
        cabinet.setAvailableNum(cabinet.getAvailableNum() - 1);
        cabinet.setUpdateTime(new Date());
        cabinetService.updateById(cabinet);
        return success("移除成功");
    }
}
